home *** CD-ROM | disk | FTP | other *** search
- Path: chronicle.mti.sgi.com!austern
- From: clamage@Eng.Sun.COM (Steve Clamage)
- Newsgroups: comp.std.c++
- Subject: Re: Calling X(int) from X()'s init list
- Date: 26 Feb 1996 09:39:12 PST
- Organization: Sun Microsystems Inc.
- Approved: austern@isolde.mti.sgi.com
- Message-ID: <4gsmnm$3aq@engnews1.Eng.Sun.COM>
- References: <4gsa2r$gjm@sdaw04.seinf.abb.se>
- Reply-To: clamage@Eng.Sun.COM
- NNTP-Posting-Host: isolde.mti.sgi.com
- X-Original-Date: 26 Feb 1996 16:22:14 GMT
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBVAwUBMTHwTEy4NqrwXLNJAQE0uwH+OTFQPxcyrHW6Wa4trbUeilR7RNWYPbzq
- MQ3WSf1qGAktWee1eG7ONyjOwG5ptUEmL7Pxj3ARTUqyrgw9ZqKEjg==
- =uA1j
- Originator: austern@isolde.mti.sgi.com
-
- In article gjm@sdaw04.seinf.abb.se, alindbac@sw.seisy.abb.se (Anders Lindback) writes:
- >In article <4gognf$et@news.bridge.net>,
- >David Byrden <100101.2547@compuserve.com> wrote:
- >><<<<<<<
-
- >>Why is it not allowed to delegate the initialisation to another
- >>constructor
-
- >>class X
- >>{
- >>public:
- >> X (int i) : i_(i) {}
- >>
- >> X () : X(34) {} // Not allowed
- >>>>>>>>>>>
- >>
- >> Why not use X (int i = 34 ) : i_(i) {}
-
- >>[ I think he was concerned with the more general case of a complicated
- >> initialization (not assignment) that must be repeated for each
- >> constructor. -sdc, moderator
- >>]
-
- >Well, one could use a similar thing using a member function:
- >
- >X (int i) { init(i); }
- >X () { init(34); }
- >
- >where init is a member function for class X.
-
- That does not solve the *initialization* problem. Consider this example:
-
- class X {
- const int i;
- ...
- public:
- X() : i(0) { ... more stuff }
- X(int k) : i(k) { ... more stuff }
- X(const X& rhs) : i(rhs.i) { ... more stuff }
- };
-
- Because member 'i' is const, it cannot be assigned to, but must be
- initialized. You cannot break out its initialization into a common
- subroutine, but must repeat the initialization on every constructor.
- In this toy example that is not a hardship, but suppose there were
- many items, and each had a more complicated initialization.
-
- Similarly, suppose 'i' were a class type that did not have a no-argument
- constructor, or a reference. You would have to provide an initializer on
- every constructor.
-
- Finally, suppose 'i' were a class type that did have a no-argument
- constructor, but you did not want the default initialization in
- this class. You could let the default initialization occur then fix
- up the value in a common subroutine. That approach is often wasteful,
- particularly if it involves allocating and freeing memory, opening
- and closing files, or setting and clearing locks.
-
- I think the original question is really about a way to break out
- a member-init-list into a common bit of code somehow, which cannot
- currently be done. It seems to me it is (only) a convenience and ease-of-
- maintenance issue.
- ---
- Steve Clamage, stephen.clamage@eng.sun.com
- ---
- [ To submit articles: Try just posting with your newsreader. If that fails,
- use mailto:std-c++@ncar.ucar.edu
- FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
- Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu
- ]
-